import 'package:flutter/material.dart';

class UserSettingsScreen extends StatefulWidget {
  const UserSettingsScreen({Key? key}) : super(key: key);

  @override
  State<UserSettingsScreen> createState() => _UserSettingsScreenState();
}

class _UserSettingsScreenState extends State<UserSettingsScreen> {
  final _formKey = GlobalKey<FormState>();
  final _nameController = TextEditingController(text: 'قەمبەر تۇرسۇن');
  final _emailController = TextEditingController(text: 'qember@example.com');
  final _phoneController = TextEditingController(text: '123-456-7890');
  String _role = 'باشقۇرغۇچى';
  bool _notificationsEnabled = true;
  bool _darkModeEnabled = false;
  bool _twoFactorEnabled = false;

  @override
  void dispose() {
    _nameController.dispose();
    _emailController.dispose();
    _phoneController.dispose();
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: const Text('ئىشلەتكۈچى تەڭشەكلىرى'),
        actions: [
          IconButton(
            icon: const Icon(Icons.save),
            onPressed: () {
              if (_formKey.currentState!.validate()) {
                ScaffoldMessenger.of(context).showSnackBar(
                  const SnackBar(content: Text('ئۆزگەرتىشلەر ساقلاندى')),
                );
                Navigator.pop(context);
              }
            },
          ),
        ],
      ),
      body: SingleChildScrollView(
        padding: const EdgeInsets.all(16.0),
        child: Form(
          key: _formKey,
          child: Column(
            crossAxisAlignment: CrossAxisAlignment.start,
            children: [
              Center(
                child: Column(
                  children: [
                    const CircleAvatar(
                      radius: 50,
                      backgroundColor: Colors.grey,
                      child: Icon(Icons.person, size: 70, color: Colors.white),
                    ),
                    const SizedBox(height: 10),
                    TextButton.icon(
                      icon: const Icon(Icons.photo_camera),
                      label: const Text('رەسىم يۈكلەش'),
                      onPressed: () {
                        // رەسىم يۈكلەش مەنتىقى
                      },
                    ),
                  ],
                ),
              ),
              const Divider(height: 30),
              const Padding(
                padding: EdgeInsets.symmetric(vertical: 8.0),
                child: Text(
                  'ئاساسىي ئۇچۇرلار',
                  style: TextStyle(fontWeight: FontWeight.bold, fontSize: 18),
                  textAlign: TextAlign.right,
                ),
              ),
              
              // ئىسىم
              Directionality(
                textDirection: TextDirection.rtl,
                child: TextFormField(
                  controller: _nameController,
                  decoration: const InputDecoration(
                    labelText: 'ئىسىم',
                    border: OutlineInputBorder(),
                    prefixIcon: Icon(Icons.person),
                  ),
                  validator: (value) {
                    if (value == null || value.isEmpty) {
                      return 'ئىسىم كىرگۈزۈڭ';
                    }
                    return null;
                  },
                ),
              ),
              const SizedBox(height: 16),
              
              // ئېلېكترونلۇق پوچتا
              Directionality(
                textDirection: TextDirection.rtl,
                child: TextFormField(
                  controller: _emailController,
                  decoration: const InputDecoration(
                    labelText: 'ئېلېكترونلۇق پوچتا',
                    border: OutlineInputBorder(),
                    prefixIcon: Icon(Icons.email),
                  ),
                  validator: (value) {
                    if (value == null || value.isEmpty) {
                      return 'ئېلېكترونلۇق پوچتا كىرگۈزۈڭ';
                    }
                    return null;
                  },
                ),
              ),
              const SizedBox(height: 16),
              
              // تېلېفون نومۇرى
              Directionality(
                textDirection: TextDirection.rtl,
                child: TextFormField(
                  controller: _phoneController,
                  decoration: const InputDecoration(
                    labelText: 'تېلېفون نومۇرى',
                    border: OutlineInputBorder(),
                    prefixIcon: Icon(Icons.phone),
                  ),
                ),
              ),
              const SizedBox(height: 16),
              
              // رولى
              Directionality(
                textDirection: TextDirection.rtl,
                child: DropdownButtonFormField<String>(
                  value: _role,
                  decoration: const InputDecoration(
                    labelText: 'رولى',
                    border: OutlineInputBorder(),
                    prefixIcon: Icon(Icons.admin_panel_settings),
                  ),
                  items: const [
                    DropdownMenuItem(value: 'باشقۇرغۇچى', child: Text('باشقۇرغۇچى')),
                    DropdownMenuItem(value: 'خىزمەتچى', child: Text('خىزمەتچى')),
                    DropdownMenuItem(value: 'ئىستېمالچى', child: Text('ئىستېمالچى')),
                  ],
                  onChanged: (value) {
                    setState(() {
                      _role = value!;
                    });
                  },
                ),
              ),
              
              const Divider(height: 30),
              const Padding(
                padding: EdgeInsets.symmetric(vertical: 8.0),
                child: Text(
                  'تەڭشەكلەر',
                  style: TextStyle(fontWeight: FontWeight.bold, fontSize: 18),
                  textAlign: TextAlign.right,
                ),
              ),
              
              // ئۇقتۇرۇشلار
              Directionality(
                textDirection: TextDirection.rtl,
                child: SwitchListTile(
                  title: const Text('ئۇقتۇرۇشلار'),
                  subtitle: const Text('سىستېمىدىن ئۇقتۇرۇش قوبۇل قىلىش'),
                  value: _notificationsEnabled,
                  onChanged: (value) {
                    setState(() {
                      _notificationsEnabled = value;
                    });
                  },
                  secondary: const Icon(Icons.notifications),
                ),
              ),
              
              // قارا ئۇسلۇب
              Directionality(
                textDirection: TextDirection.rtl,
                child: SwitchListTile(
                  title: const Text('قارا ئۇسلۇب'),
                  subtitle: const Text('قارا تېما ئىشلىتىش'),
                  value: _darkModeEnabled,
                  onChanged: (value) {
                    setState(() {
                      _darkModeEnabled = value;
                    });
                  },
                  secondary: const Icon(Icons.dark_mode),
                ),
              ),
              
              // قوش ئامىللىق دەلىللەش
              Directionality(
                textDirection: TextDirection.rtl,
                child: SwitchListTile(
                  title: const Text('قوش ئامىللىق دەلىللەش'),
                  subtitle: const Text('ھېساباتىڭىزنى قوغداش ئۈچۈن قوش ئامىللىق دەلىللەشنى قوزغىتىڭ'),
                  value: _twoFactorEnabled,
                  onChanged: (value) {
                    setState(() {
                      _twoFactorEnabled = value;
                    });
                  },
                  secondary: const Icon(Icons.security),
                ),
              ),
              
              const Divider(height: 30),
              const Padding(
                padding: EdgeInsets.symmetric(vertical: 8.0),
                child: Text(
                  'بىخەتەرلىك',
                  style: TextStyle(fontWeight: FontWeight.bold, fontSize: 18),
                  textAlign: TextAlign.right,
                ),
              ),
              
              // پارول ئۆزگەرتىش
              Directionality(
                textDirection: TextDirection.rtl,
                child: ListTile(
                  leading: const Icon(Icons.lock),
                  title: const Text('پارول ئۆزگەرتىش'),
                  trailing: const Icon(Icons.chevron_right),
                  onTap: () {
                    // پارول ئۆزگەرتىش دىئالوگىنى كۆرسىتىش
                    _showChangePasswordDialog(context);
                  },
                ),
              ),
              
              const SizedBox(height: 20),
              Center(
                child: ElevatedButton.icon(
                  icon: const Icon(Icons.save),
                  label: const Text('ساقلاش'),
                  style: ElevatedButton.styleFrom(
                    padding: const EdgeInsets.symmetric(horizontal: 30, vertical: 15),
                  ),
                  onPressed: () {
                    if (_formKey.currentState!.validate()) {
                      ScaffoldMessenger.of(context).showSnackBar(
                        const SnackBar(content: Text('ئۆزگەرتىشلەر ساقلاندى')),
                      );
                      Navigator.pop(context);
                    }
                  },
                ),
              ),
            ],
          ),
        ),
      ),
    );
  }
  
  void _showChangePasswordDialog(BuildContext context) {
    final currentPasswordController = TextEditingController();
    final newPasswordController = TextEditingController();
    final confirmPasswordController = TextEditingController();
    
    showDialog(
      context: context,
      builder: (context) => AlertDialog(
        title: const Text('پارول ئۆزگەرتىش'),
        content: SingleChildScrollView(
          child: Directionality(
            textDirection: TextDirection.rtl,
            child: Column(
              mainAxisSize: MainAxisSize.min,
              children: [
                TextFormField(
                  controller: currentPasswordController,
                  obscureText: true,
                  decoration: const InputDecoration(
                    labelText: 'ھازىرقى پارول',
                    border: OutlineInputBorder(),
                  ),
                ),
                const SizedBox(height: 16),
                TextFormField(
                  controller: newPasswordController,
                  obscureText: true,
                  decoration: const InputDecoration(
                    labelText: 'يېڭى پارول',
                    border: OutlineInputBorder(),
                  ),
                ),
                const SizedBox(height: 16),
                TextFormField(
                  controller: confirmPasswordController,
                  obscureText: true,
                  decoration: const InputDecoration(
                    labelText: 'يېڭى پارولنى قايتا كىرگۈزۈڭ',
                    border: OutlineInputBorder(),
                  ),
                ),
              ],
            ),
          ),
        ),
        actions: [
          TextButton(
            onPressed: () {
              Navigator.pop(context);
            },
            child: const Text('بىكار قىلىش'),
          ),
          ElevatedButton(
            onPressed: () {
              // پارول ئۆزگەرتىش مەنتىقى
              if (newPasswordController.text == confirmPasswordController.text) {
                Navigator.pop(context);
                ScaffoldMessenger.of(context).showSnackBar(
                  const SnackBar(content: Text('پارول مۇۋەپپەقىيەتلىك ئۆزگەرتىلدى')),
                );
              } else {
                ScaffoldMessenger.of(context).showSnackBar(
                  const SnackBar(content: Text('يېڭى پاروللار ماس كەلمىدى')),
                );
              }
            },
            child: const Text('ساقلاش'),
          ),
        ],
      ),
    );
  }
} 